import requests
from bs4 import BeautifulSoup

def get_imei_info(query):
    # 构造目标 URL
    url = f"https://market.quectel.com/imei-verification/?query={query}"

    # 发送 HTTP 请求
    response = requests.get(url)

    # 检查请求是否成功
    if response.status_code == 200:
        # 解析 HTML 内容
        soup = BeautifulSoup(response.content, 'html.parser')
        
        # 查找包含数据的 <div> 元素
        data_div = soup.find('div', class_='bg-light p-3')
        
        # 初始化一个字典来保存结果
        result = {
            'SW': None,
            'OC': None,
            'SN': None,
            'IMEI': None,
            'MEID': None
        }
        
        # 解析表格内容
        if data_div:
            table = data_div.find('table')
            if table:
                rows = table.find_all('tr')
                for row in rows:
                    cols = row.find_all('th')
                    if cols:
                        key = cols[0].text.strip().replace('：', '')  # 去掉冒号
                        value = row.find('td').find('label').text.strip()
                        if key in result:
                            result[key] = value
            
            # 提取 My IP（可选）
            ip_label = data_div.find('strong', string='My IP:')
            if ip_label:
                result['My IP'] = ip_label.next_sibling.strip()
        
        # 打印结果
        print("提取到的数据：")
        for key, value in result.items():
            print(f"{key}: {value}")
    else:
        print(f"请求失败，状态码：{response.status_code}")

# 用户输入 IMEI 号码
if __name__ == "__main__":
    imei_query = input("请输入 IMEI 号码：")
    get_imei_info(imei_query)

